package com.coldwindblows.blog.utils;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import com.coldwindblows.blog.constant.WebConst;
import com.coldwindblows.blog.pojo.ContentDo;
import com.github.pagehelper.PageInfo;
import com.vdurmont.emoji.EmojiParser;

/**
 * 主题公共函数
 * Created by ColdWindBlows on 2018/07/05
 */
@Component
public final class Commons {

	private static final String[] ICONS = {"bg-ico-book", "bg-ico-game", "bg-ico-note", "bg-ico-char", "bg-ico-code", "bg-ico-image", "bg-ico-web", "bg-ico-link", "bg-ico-design", "bg-ico-lock"};
	
	public static String THEME = "themes/default";
	
	/**
	 * 判断分页中是否有数据
	 * @param paginator
	 * @return
	 */
	public static boolean is_empty(PageInfo<Object> paginator) {
		return paginator == null || (paginator.getList() == null) || (paginator.getList().size() == 0);
	}
	
	/**
	 * 网站链接
	 * @return
	 */
	public static String site_url() {
		return site_url("");
	}
	
	/**
	 * 返回网站链接下的全址
	 * @param sub
	 * @return
	 */
	public static String site_url(String sub) {
		return site_option("site_url") + sub;
	}
	
	/**
	 * 网站标题
	 * @return
	 */
	public static String site_title() {
		return site_option("site_title");
	}
	
	/**
	 * 网站配置项
	 * @param key
	 * @return
	 */
	public static String site_option(String key) {
		return site_option(key, "");
	}
	
	/**
	 * 网站配置项
	 * @param key
	 * @param defaultValue
	 * @return
	 */
	public static String site_option(String key, String defaultValue) {
		if(StringUtils.isBlank(key)) {
			return "";
		}
		
		String str = WebConst.initConfig.get(key);
		if(StringUtils.isNotBlank(str)) {
			return str;
		} else {
			return defaultValue;
		}
	}
	
	/**
	 * 截取字符串
	 * @param str
	 * @param len
	 * @return
	 */
	public static String subStr(String str, int len) {
		if(str.length() > len) {
			return str.substring(0, len);
		}
		return str;
	}
	
	/**
	 * 返回主题URL
	 * @return
	 */
	public static String theme_url() {
		return site_url(Commons.THEME);
	}
	
	/**
	 * 返回主题i下的文件路径
	 * @param sub
	 * @return
	 */
	public static String theme_url(String sub) {
		return site_url(Commons.THEME + sub);
	}
	
	/**
	 * 返回github头像地址
	 * @param email
	 * @return
	 */
	public static String gravatar(String email) {
		String avatarUrl = "https://github.com/identicons/";
		if(StringUtils.isBlank(email)) {
			email = "user@hanshuai.xin";
		}
		String hash = TaleUtils.MD5encode(email.trim().toLowerCase());
		return avatarUrl + hash + ".png";
	}
	
	/**
	 * 返回文章链接地址
	 * @param content
	 * @return
	 */
	public static String permalink(ContentDo content) {
		return permalink(content.getCid(), content.getSlug());
	}
	
	 /**
     * 格式化unix时间戳为日期
     *
     * @param unixTime
     * @return
     */
    public static String fmtdate(Integer unixTime) {
        return fmtdate(unixTime, "yyyy-MM-dd");
    }
    
    public static String fmtdate(Date date) {
    	return fmtdate(date, "yyyy-MM-dd");
    }

    /**
     * 格式化unix时间戳为日期
     *
     * @param unixTime
     * @param patten
     * @return
     */
    public static String fmtdate(Integer unixTime, String patten) {
        if (null != unixTime && StringUtils.isNotBlank(patten)) {
            return TimeUtil.toTime(new Date((long)(unixTime * 1000)), patten);
        }
        return "";
    }
    
    public static String fmtdate(Date date, String pattern) {
    	if (null != date) {
            return TimeUtil.toTime(date, pattern);
        }
        return "";
    }
	
	/**
	 * 获取随机数
	 * @param max
	 * @param str
	 * @return
	 */
	public static String random(int max, String str) {
		return UUID.random(1, max) + str;
	}
	
	/**
	 * 返回文章链接地址
	 * @param cid
	 * @param slug
	 * @return
	 */
	public static String permalink(Integer cid, String slug) {
		return site_url("/article/" + (StringUtils.isNotBlank(slug) ? slug : cid.toString()));
	}
	
	/**
	 * 显示分类
	 * @param categories
	 * @return
	 * @throws UnsupportedEncodingException 
	 */
	public static String show_categories(String categories) throws UnsupportedEncodingException {
		if(StringUtils.isNotBlank(categories)) {
			String[] arr = categories.split(",");
			StringBuffer sbuf = new StringBuffer();
			for(String c : arr) {
				sbuf.append("<a href=\"/category/" + URLEncoder.encode(c, "UTF-8") + "\">" + c + "</a>");
			}
			return sbuf.toString();
		}
		return show_categories("默认分类");
	}
	
	/**
	 * 显示标签
	 * @param tags
	 * @return
	 * @throws UnsupportedEncodingException 
	 */
	public static String show_tags(String tags) throws UnsupportedEncodingException {
		if(StringUtils.isNotBlank(tags)) {
			String[] arr = tags.split(",");
			StringBuffer subf = new StringBuffer();
			for(String c : arr) {
				subf.append("<a href=\"/tag/" + URLEncoder.encode(c, "UTF-8") + "\">" + c + "</a>");
			}
			return subf.toString();
		}
		return "";
	}
	
	/**
	 * 截取文章摘要
	 * @param value
	 * @param len
	 * @return
	 */
	public static String intro(String value, int len) {
		int pos = value.indexOf("<!--more-->");
		if(pos != -1) {
			String html = value.substring(0, pos);
			return TaleUtils.htmlToText(TaleUtils.mdToHtml(html));
		} else {
			String text = TaleUtils.htmlToText(TaleUtils.mdToHtml(value));
			if(text.length() > len) {
				return text.substring(0, len);
			}
			return text;
		}
	}
	
	/**
	 * 显示文章内容，转换markdown为html
	 * @param value
	 * @return
	 */
	public static String article(String value) {
		if(StringUtils.isNotBlank(value)) {
			value = value.replace("<!--more-->", "\r\n");
			return TaleUtils.mdToHtml(value);
		}
		return "";
	}
	
	/**
	 * 显示文章缩略图，顺序为文章第一张图-》随机获取
	 * @param content
	 * @return
	 */
	public static String show_thumb(ContentDo content) {
		int cid = content.getCid();
		int size = cid % 20;
		size = size == 0 ? 1 : size;
		
		return "/user/img/rand/" + size + ".jpg";
	}
	
	/**
	 * 这种格式的字符转换为emoji表情
	 * @param value
	 * @return
	 */
	public static String emoji(String value) {
		return EmojiParser.parseToUnicode(value);
	}
	
	/**
	 * 获取文章第一张图片
	 * @param content
	 * @return
	 */
	public static String show_thumb(String content) {
		content = TaleUtils.mdToHtml(content);
		if(content.contains("<img")) {
			String img = "";
			String regexImg = "<img.*src\\s*=\\s*=(.*?)[^>]*?>";
			Pattern pImage = Pattern.compile(regexImg, Pattern.CASE_INSENSITIVE);
			Matcher mImage = pImage.matcher(content);
			if(mImage.find()) {
				img = img + "," + mImage.group();
				// 匹配src
				Matcher m = Pattern.compile("src\\s*=\\s*=\'?\"?(.*?)(\'|\"|>|\\s+)").matcher(img);
				if (m.find()) {
					return m.group(1);
				}
			}
		}
		return "";
	}
	
	/**
	 * 显示文章图标
	 * @param cid
	 * @return
	 */
	public static String show_icon(int cid) {
		return ICONS[cid % ICONS.length];
	}
	
	/**
	 * 获取社交的链接地址
	 * @return
	 */
	public static Map<String, String> social() {
		final String prefix = "social_";
		Map<String, String> map = new HashMap<>(4);
		map.put("weibo", WebConst.initConfig.get(prefix + "weibo"));
		map.put("zhihu", WebConst.initConfig.get(prefix + "zhihu"));
		map.put("github", WebConst.initConfig.get(prefix + "github"));
		map.put("twitter", WebConst.initConfig.get(prefix + "twitter"));
		return map;
	}
	
}
